package com.binance.amazon.s3.config;


import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.EC2ContainerCredentialsProviderWrapper;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.lambda.AWSLambda;
import com.amazonaws.services.lambda.AWSLambdaClientBuilder;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;

@Configuration
@Log4j2
public class AmazonLambdaConfig {

    private Environment environment;

    @Value("${amazon.lambda.region:null}")
    private String region;

    @Value("${amazon.lambda.accesskey:null}")
    private String accessKey;

    @Value("${amazon.lambda.secretkey:null}")
    private String secretKey;


    @Bean("awsLambda")
    public AWSLambda awsLambda() {
        if (StringUtils.isEmpty(region)) {
            return null;
        }
        // (2) Instantiate AWSLambdaClientBuilder to build the Lambda client
        AWSLambdaClientBuilder builder = AWSLambdaClientBuilder.standard()
                .withRegion(Regions.fromName(region));
        if (StringUtils.isEmpty(accessKey) || StringUtils.isEmpty(secretKey)) {
            log.info("accessKey或secretKey为空, EC2ContainerCredentialsProviderWrapper");
            builder.withCredentials(new EC2ContainerCredentialsProviderWrapper());
        } else {
            log.info("accessKey和secretKe不为空, 使用AWSStaticCredentialsProvider认证");
            builder.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)));
        }

        // (3) Build the client, which will ultimately invoke the function
        AWSLambda client = builder.build();
        return client;
    }
}
